package com.ems.controller;

import com.ems.entity.TRoler;
import com.ems.entity.TUser;
import com.ems.service.TRolerService;
import com.ems.service.TUserService;
import com.ems.util.Response;
import com.ems.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZZH
 * @since 2022-06-27
 */
@RestController
@RequestMapping("/t-roler")
public class TRolerController {
    @Autowired
    private TRolerService tRolerService;

    //新增角色数据
    @PostMapping("/add")
    public Response add(TRoler tRoler){
        if(tRolerService.findOne(tRoler.getId())==null){
            tRolerService.add(tRoler);
            return new Response(200,"添加成功","",null);
        }else {
            return new Response(400,"角色已存在","",null);
        }
    }

    //删除角色数据
    @GetMapping("/delete/{id}")
    public Response delete(@PathVariable Integer id){
        if(tRolerService.findOne(id)!=null){
            tRolerService.delete(id);
            return new Response(200,"删除成功","",null);
        }else {
            return new Response(400,"角色不存在","",null);
        }
    }


    //修改角色数据
    @PostMapping("/update")
    public ResultUtil updateRoler(TRoler tRoler) {
        if (tRolerService.findOne(tRoler.getId()) == null) {
            return ResultUtil.fail("角色不存在");
        } else {
            tRolerService.update(tRoler);
            return ResultUtil.success("修改成功");
        }
    }

    //查找角色数据(单)
    @GetMapping("/findOne/{id}")
    public ResultUtil get(@PathVariable Integer id){
        if (tRolerService.findOne(id)==null){
            return ResultUtil.fail("角色不存在");
        }else {
            return ResultUtil.success(tRolerService.findOne(id));
        }
    }


    //查找角色数据(打印所有)
    @GetMapping("/findAll")
    public ResultUtil findAll() {
        if (tRolerService.findAll() == null) {
            return ResultUtil.fail("查找失败");
        } else {
            return ResultUtil.success(tRolerService.findAll());
        }
    }
}
